# Load Necessary Packages
library(bcf)
library(dbarts)

# Load Datasets
setwd("/users/josephphillips/Dropbox/COVID-19/Data/FC Data Repository")
usa <- read.csv("USA.csv",header=T,sep=",")
uk <- read.csv("UK.csv",header=T,sep=",")
canada <- read.csv("Canada 1.csv",header=T,sep=",")
canada2 <- read.csv("Canada 2.csv",header=T,sep=",")

# US, W2-W2
data.nonmissing.w2.target.false <- subset(usa,is.na(usa$W2_target_false)==F & is.na(usa$ideo7)==F & is.na(usa$health_trust)==F & is.na(usa$target_false)==F & is.na(usa$pid3)==F & is.na(usa$approve_trmp)==F & is.na(usa$therm_trump)==F & is.na(usa$conspiracy)==F & is.na(usa$media_trust)==F)
bcf_fit_w2tf <- bcf(data.nonmissing.w2.target.false$W2_target_false,data.nonmissing.w2.target.false$factcheck_treatment,x_control=as.matrix(data.nonmissing.w2.target.false[,c("ideo7","health_trust","target_false")]),x_moderate=as.matrix(data.nonmissing.w2.target.false[,c("pid3","approve_trmp","therm_trump","health_trust","conspiracy","media_trust","target_false")]),pihat=data.nonmissing.w2.target.false$factcheck_treatment,nburn=5000,nsim=20000,update_interval=10000)
tau_post <- bcf_fit_w2tf$tau
tauhat <- colMeans(tau_post)
data.nonmissing.w2.target.false$individual_effects <- tauhat

# US, W2-W3
data.nonmissing.w2w3.target.false <- subset(usa,is.na(usa$W3_target_false)==F & is.na(usa$ideo7)==F & is.na(usa$health_trust)==F & is.na(usa$target_false)==F & is.na(usa$pid3)==F & is.na(usa$approve_trmp)==F & is.na(usa$therm_trump)==F & is.na(usa$conspiracy)==F & is.na(usa$media_trust)==F)
bcf_fit_w2w3tf <- bcf(data.nonmissing.w2w3.target.false$W3_target_false,data.nonmissing.w2w3.target.false$factcheck_treatment,x_control=as.matrix(data.nonmissing.w2w3.target.false[,c("pid3","ideo7","health_trust","target_false")]),x_moderate=as.matrix(data.nonmissing.w2w3.target.false[,c("pid3","approve_trmp","therm_trump","health_trust","conspiracy","media_trust","target_false")]),pihat=data.nonmissing.w2w3.target.false$factcheck_treatment,nburn=5000,nsim=20000,update_interval=1000)
tau_post <- bcf_fit_w2w3tf$tau
tauhat <- colMeans(tau_post)
data.nonmissing.w2w3.target.false$individual_effects <- tauhat

# US, W2-W4
data.nonmissing.w2w4.target.false <- subset(usa,is.na(usa$W4_target_false)==F & is.na(usa$ideo7)==F & is.na(usa$health_trust)==F & is.na(usa$target_false)==F & is.na(usa$pid3)==F & is.na(usa$approve_trmp)==F & is.na(usa$therm_trump)==F & is.na(usa$conspiracy)==F & is.na(usa$media_trust)==F)
bcf_fit_w2w4tf <- bcf(data.nonmissing.w2w4.target.false$W4_target_false,data.nonmissing.w2w4.target.false$factcheck_treatment,x_control=as.matrix(data.nonmissing.w2w4.target.false[,c("pid3","ideo7","health_trust","target_false")]),x_moderate=as.matrix(data.nonmissing.w2w4.target.false[,c("pid3","approve_trmp","therm_trump","health_trust","conspiracy","media_trust","target_false")]),pihat=data.nonmissing.w2w4.target.false$factcheck_treatment,nburn=5000,nsim=20000,update_interval=1000)
tau_post <- bcf_fit_w2w4tf$tau
tauhat <- colMeans(tau_post)
data.nonmissing.w2w4.target.false$individual_effects <- tauhat

# US, W3-W3
data.nonmissing.w3w3.target.false <- subset(usa,is.na(usa$W3_target_false)==F & is.na(usa$ideo7)==F & is.na(usa$health_trust)==F & is.na(usa$target_false)==F & is.na(usa$pid3)==F & is.na(usa$approve_trmp)==F & is.na(usa$therm_trump)==F & is.na(usa$conspiracy)==F & is.na(usa$media_trust)==F)
bcf_fit_w3w3tf <- bcf(data.nonmissing.w3w3.target.false$W3_target_false,data.nonmissing.w3w3.target.false$W3factcheck_treatment,x_control=as.matrix(data.nonmissing.w3w3.target.false[,c("pid3","ideo7","health_trust","target_false")]),x_moderate=as.matrix(data.nonmissing.w3w3.target.false[,c("pid3","approve_trmp","therm_trump","health_trust","conspiracy","media_trust","target_false")]),pihat=data.nonmissing.w3w3.target.false$W3factcheck_treatment,nburn=5000,nsim=20000,update_interval=1000)
tau_post <- bcf_fit_w3w3tf$tau
tauhat <- colMeans(tau_post)
data.nonmissing.w3w3.target.false$individual_effects <- tauhat

# US, W3-W4
data.nonmissing.w3w4.target.false <- subset(usa,is.na(usa$W3_target_false)==F & is.na(usa$ideo7)==F & is.na(usa$health_trust)==F & is.na(usa$target_false)==F & is.na(usa$pid3)==F & is.na(usa$approve_trmp)==F & is.na(usa$therm_trump)==F & is.na(usa$conspiracy)==F & is.na(usa$media_trust)==F)
bcf_fit_w3w4tf <- bcf(data.nonmissing.w3w4.target.false$W3_target_false,data.nonmissing.w3w4.target.false$W3factcheck_treatment,x_control=as.matrix(data.nonmissing.w3w4.target.false[,c("pid3","ideo7","health_trust","target_false")]),x_moderate=as.matrix(data.nonmissing.w3w4.target.false[,c("pid3","approve_trmp","therm_trump","health_trust","conspiracy","media_trust","target_false")]),pihat=data.nonmissing.w3w3.target.false$W3factcheck_treatment,nburn=5000,nsim=20000,update_interval=1000)
tau_post <- bcf_fit_w3w4tf$tau
tauhat <- colMeans(tau_post)
data.nonmissing.w3w4.target.false$individual_effects <- tauhat

# UK, W2-W2
data.nonmissing.w2.target.false <- subset(uk,is.na(uk$W2_targeted_false)==F & is.na(uk$knowledge)==F & is.na(uk$targeted_false)==F & is.na(uk$left)==F & is.na(uk$right)==F & is.na(uk$approve_Boris)==F & is.na(uk$therm_boris)==F & is.na(uk$trust_healthgov)==F & is.na(uk$conspiracy_general)==F & is.na(uk$total_media_trust)==F)
bcf_fit_w2tf <- bcf(data.nonmissing.w2.target.false$W2_targeted_false,data.nonmissing.w2.target.false$factcheck_treatment,x_control=as.matrix(data.nonmissing.w2.target.false[,c("knowledge","targeted_false")]),x_moderate=as.matrix(data.nonmissing.w2.target.false[,c("left","right","approve_Boris","therm_boris","trust_healthgov","conspiracy_general","total_media_trust","targeted_false")]),pihat=data.nonmissing.w2.target.false$factcheck_treatment,nburn=5000,nsim=20000,update_interval=1000)
tau_post <- bcf_fit_w2tf$tau
tauhat <- colMeans(tau_post)
data.nonmissing.w2.target.false$individual_effects <- tauhat

# UK, W2-W3
data.nonmissing.w3.target.false <- subset(uk,is.na(uk$W3_targeted_false)==F & is.na(uk$targeted_false)==F & is.na(uk$left)==F & is.na(uk$right)==F & is.na(uk$approve_Boris)==F & is.na(uk$therm_boris)==F & is.na(uk$trust_healthgov)==F & is.na(uk$conspiracy_general)==F & is.na(uk$total_media_trust)==F)
bcf_fit_w2w3tf <- bcf(data.nonmissing.w3.target.false$W3_targeted_false,data.nonmissing.w3.target.false$factcheck_treatment,x_control=as.matrix(data.nonmissing.w3.target.false[,c("trust_healthgov","targeted_false")]),x_moderate=as.matrix(data.nonmissing.w3.target.false[,c("left","right","approve_Boris","therm_boris","trust_healthgov","conspiracy_general","total_media_trust","targeted_false")]),pihat=data.nonmissing.w3.target.false$factcheck_treatment,nburn=5000,nsim=20000,update_interval=1000)
tau_post <- bcf_fit_w2w3tf$tau
tauhat <- colMeans(tau_post)
data.nonmissing.w3.target.false$W2_individual_effects <- tauhat

# UK, W3-W3
data.nonmissing.w3.target.false$trt_three <- data.nonmissing.w3.target.false$W3factcheck_treatment
bcf_fit_w3w3tf <- bcf(data.nonmissing.w3.target.false$W3_targeted_false,data.nonmissing.w3.target.false$trt_three,x_control=as.matrix(data.nonmissing.w3.target.false[,c("trust_healthgov","targeted_false")]),x_moderate=as.matrix(data.nonmissing.w3.target.false[,c("left","right","approve_Boris","therm_boris","trust_healthgov","conspiracy_general","total_media_trust","targeted_false")]),pihat=data.nonmissing.w3.target.false$trt_three,nburn=5000,nsim=20000,update_interval=1000)
tau_post <- bcf_fit_w3w3tf$tau
tauhat <- colMeans(tau_post)
data.nonmissing.w3.target.false$W3_individual_effects <- tauhat

# Canada 1
data.nonmissing <- subset(canada,is.na(canada$targeted_false)==F & is.na(canada$university)==F & is.na(canada$age55)==F & is.na(canada$age65)==F & is.na(canada$male)==F & is.na(canada$frequentchurch)==F & is.na(canada$west)==F & is.na(canada$right)==F & is.na(canada$ideology1)==F & is.na(canada$knowledge)==F & is.na(canada$nonwhite)==F & is.na(canada$polinterest)==F & is.na(canada$trust_healthgov)==F & is.na(canada$total_media_trust)==F & is.na(canada$left)==F & is.na(canada$right)==F & is.na(canada$approve_trudeau)==F & is.na(canada$trudeau_therm)==F & is.na(canada$conspiracy)==0)
bcf_fit <- bcf(data.nonmissing$targeted_false,data.nonmissing$treat,x_control=as.matrix(data.nonmissing[,c("university","age55","age65","male","frequentchurch","west","right","ideology1","knowledge","nonwhite","polinterest","trust_healthgov","total_media_trust")]),x_moderate=as.matrix(data.nonmissing[,c("left","right","approve_trudeau","trudeau_therm","trust_healthgov","conspiracy","total_media_trust")]),pihat=data.nonmissing$treat,nburn=5000,nsim=20000,update_interval=1000)
tau_post <- bcf_fit$tau
tauhat <- colMeans(tau_post)
data.nonmissing$individual_effects <- tauhat

# Canada 2
data.nonmissing <- subset(canada2,is.na(canada2$targeted_false)==F & is.na(canada2$age45)==F & is.na(canada2$age55)==F & is.na(canada2$left)==F & is.na(canada2$right)==F & is.na(canada2$polinterest)==F & is.na(canada2$age65)==F & is.na(canada2$male)==F & is.na(canada2$frequentchurch)==F  & is.na(canada2$ideology1)==F & is.na(canada2$knowledge)==F & is.na(canada2$nonwhite)==F &  is.na(canada2$trust_healthgov)==F & is.na(canada2$total_media_trust)==F & is.na(canada2$left)==F & is.na(canada2$right)==F & is.na(canada2$approve_trudeau)==F & is.na(canada2$trudeau_therm)==F & is.na(canada2$conspiracy)==0)
bcf_fit <- bcf(data.nonmissing$targeted_false,data.nonmissing$treat,x_control=as.matrix(data.nonmissing[,c("age45","age55","age65","male","left","right","frequentchurch","ideology1","knowledge","nonwhite","polinterest","trust_healthgov","total_media_trust")]),x_moderate=as.matrix(data.nonmissing[,c("left","right","approve_trudeau","trudeau_therm","trust_healthgov","conspiracy","total_media_trust")]),pihat=data.nonmissing$treat,nburn=5000,nsim=20000,update_interval=1000)
tau_post <- bcf_fit$tau
tauhat <- colMeans(tau_post)
data.nonmissing$individual_effects <- tauhat
